package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.CodeGenerator;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.*;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.vo.CheckInElderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 入住Service业务层处理
 * 
 * @author Winnie
 * @date 2025-11-11
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper,CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;

    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return save(checkIn)?1:0;
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return updateById(checkIn)?1:0;
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    @Override
    public void apply(CheckInApplyDto dto) {
        List<ElderFamilyDto> elderFamilyDtoList = dto.getElderFamilyDtoList();
        CheckInContractDto contractDto = dto.getCheckInContractDto();
        CheckInConfigDto configDto = dto.getCheckInConfigDto();
        CheckInElderDto elderDto = dto.getCheckInElderDto();

        //校验老人是否已入住  如果已入住直接抛异常BaseException
        LambdaQueryWrapper<Elder> elder = new LambdaQueryWrapper<Elder>().eq(Elder::getIdCardNo, elderDto.getIdCardNo()).eq(Elder::getStatus, 4);
        Elder one = elderMapper.selectOne(elder);
        if (one != null) {
            throw new RuntimeException("该老人已入住");
        }
        //更新床位的状态
        Long bedId = configDto.getBedId();
        Bed bed = bedMapper.selectById(bedId);
        if (bed == null){
            throw new RuntimeException("床位不存在");
        }
        bed.setBedStatus(1);
        bedMapper.updateById(bed);

        //新增或更新老人elder
        saveOrUpdateElder(elderDto,bed);

        //新增合同信息  合同号要求： HT+18位数字，例：HT202501011012590001
        saveContract(contractDto,elderDto,configDto);
        //新增入住信息  老人家属信息存到remark字段中
        saveCheckIn(contractDto,elderDto,one,configDto);
        //新增入住配置
        saveCheckInConfig(configDto,elderDto,contractDto);
    }

//    @Override
//    public CheckInElderInfoVo detail(Long id) {
//
//
//    }

    private void saveCheckInConfig(CheckInConfigDto configDto, CheckInElderDto elderDto, CheckInContractDto contractDto) {
        CheckInConfig checkInConfig = new CheckInConfig();
        checkInConfig.setCheckInId(checkInConfig.getCheckInId());
        checkInConfig.setNursingLevelId(checkInConfig.getNursingLevelId());
        checkInConfig.setNursingLevelName(checkInConfig.getNursingLevelName());
        checkInConfig.setFeeStartDate(configDto.getFeeStartDate());
        checkInConfig.setFeeEndDate(checkInConfig.getFeeEndDate());
        checkInConfig.setDeposit(checkInConfig.getDeposit());
        checkInConfig.setNursingFee(checkInConfig.getNursingFee());
        checkInConfig.setBedFee(checkInConfig.getBedFee());
        checkInConfig.setInsurancePayment(checkInConfig.getInsurancePayment());
        checkInConfig.setGovernmentSubsidy(checkInConfig.getGovernmentSubsidy());
        checkInConfig.setOtherFees(checkInConfig.getOtherFees());
        checkInConfigMapper.insert(checkInConfig);

    }

    private void saveCheckIn(CheckInContractDto contractDto, CheckInElderDto elderDto, Elder  one, CheckInConfigDto configDto) {
        CheckIn checkIn = new CheckIn();
        checkIn.setElderName(elderDto.getName());
        checkIn.setElderId(one.getId());
        checkIn.setIdCardNo(one.getIdCardNo());
        checkIn.setStartDate(configDto.getStartDate());
        checkIn.setEndDate(configDto.getEndDate());
        checkIn.setNursingLevelName(configDto.getNursingLevelName());
        checkIn.setBedNumber(configDto.getBedId());
        checkIn.setStatus(1);
        checkInMapper.insert(checkIn);

    }

    private void saveContract(CheckInContractDto contractDto, CheckInElderDto elderDto, CheckInConfigDto configDto) {
        Contract contract = new Contract();
        contract.setContractName(contractDto.getContractName()) ;
        contract.setContractNumber(CodeGenerator.generateContractNumber());
        contract.setAgreementPath(contractDto.getAgreementPath());
        contract.setThirdPartyPhone(contractDto.getThirdPartyPhone());
        contract.setThirdPartyName(contractDto.getThirdPartyName());
        contract.setElderName(elderDto.getName());
        contract.setStartDate(configDto.getStartDate());
        contract.setEndDate(configDto.getEndDate());
        contract.setStatus(1);
        contract.setSignDate(contractDto.getSignDate());
        contractMapper.insert(contract);

    }

    private void saveOrUpdateElder(CheckInElderDto elderDto,Bed  bed) {
        //查询老人是否入住
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<Elder>().eq(Elder::getIdCardNo, elderDto.getIdCardNo());
        Elder one = elderMapper.selectOne(queryWrapper);
        if (one == null) {
            Elder elder = new Elder();
            elder.setName(elderDto.getName());
            elder.setImage(elderDto.getImage());
            elder.setIdCardNo(elderDto.getIdCardNo());
            elder.setSex(elderDto.getSex());
            elder.setStatus(4);
            elder.setPhone(elderDto.getPhone());
            elder.setBirthday(elderDto.getBirthday());
            elder.setAddress(elderDto.getAddress());
            elder.setIdCardNationalEmblemImg(elderDto.getIdCardNationalEmblemImg());
            elder.setIdCardPortraitImg(elderDto.getIdCardPortraitImg());
            elder.setBedNumber(bed.getBedNumber());
            elder.setBedId(bed.getId());
            elderMapper.insert(elder);
        }else {
            Elder elder = new Elder();
            elder.setName(elderDto.getName());
            elder.setImage(elderDto.getImage());
            elder.setIdCardNo(elderDto.getIdCardNo());
            elder.setSex(elderDto.getSex());
            elder.setStatus(4);
            elder.setPhone(elderDto.getPhone());
            elder.setBirthday(elderDto.getBirthday());
            elder.setAddress(elderDto.getAddress());
            elder.setIdCardNationalEmblemImg(elderDto.getIdCardNationalEmblemImg());
            elder.setIdCardPortraitImg(elderDto.getIdCardPortraitImg());
            elder.setBedNumber(bed.getBedNumber());
            elder.setBedId(bed.getId());
            elderMapper.updateById(elder);
        }
    }

}
